<template>
<el-form style="width: 60% " :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="图书名字" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
    </el-form-item>
    <el-form-item label="图书作者" prop="author">
        <el-input v-model="ruleForm.author"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
</el-form>
</template>
<script>
    export default {
        data() {
            return {
                ruleForm: {
                    name: '',
                    author: ''
                },
                rules: {
                    name: [
                        { required: true, message: '请输入图书名称-长度在 3 到 8 个字符', trigger: 'blur' },
                        { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
                    ],
                    author: [
                        { required: true, message: '请输入作者名称', trigger: 'blur' },
                        // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                    ]

                }
            };
        },
        methods: {
            submitForm(formName) {
                const _this=this
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       axios.post('http://localhost:8181/book/save',this.ruleForm).then(function (response) {
                           if (response.data = 'success') {
                               // alert("添加成功")
                               //_this.$message('添加成功');
                               _this.$alert('书名:'+_this.ruleForm.name+'   作者:'+_this.ruleForm.author, '添加成功！', {
                                   confirmButtonText: '确定',
                                   callback: action => {
                                       _this.$router.push('/BookManager')
                                   }
                               });

                           }
                       })
                    } else {
                        console.log('校验不通过');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            }
        }
    }
</script>